package com.example.chapter05;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {

    private EditText et_phone;
    private EditText et_pw;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_focus);
        et_phone = findViewById(R.id.et_phone);
        et_pw = findViewById(R.id.et_pw);
        button = findViewById(R.id.btn_login);
        et_phone.setOnFocusChangeListener(this);
        et_pw.setOnFocusChangeListener(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String password = et_pw.getText().toString();
                if (TextUtils.isEmpty(password) ||password.length()<6){
                    et_pw.requestFocus();
                    Toast.makeText(EditFocusActivity.this, "请输入6位数密码", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            String phone = et_phone.getText().toString();
            //手机号码不足11位
            if (TextUtils.isEmpty(phone) || phone.length()<11){
                //手机号码编辑框请求获取焦点，也就是把光标移到手机号码编辑框
                et_phone.requestFocus();
                Toast.makeText(this, "请输入11位手机号码", Toast.LENGTH_SHORT).show();
                
            }
        }
    }
}